var CustomerMixin = function (customerType) {
  return {
    data: function () {
      // 搜索条件
      var search = {
        customer:'',
        paymentWay: '',
        status: '使用'
      };
      return {
        loading: false,
        isShowCustomerAddForm: false,
        customerList: [],
        updateCustomer: new Customer(),
        addCustomer: new Customer(),
        search: _.cloneDeep(search),
        oldSearch: _.cloneDeep(search),
        pagination: {
          currentPage: 1,
          totalCount: 0,
          pageSize: 15
        }
      };
    },

    created: function () {
      this.setCustomerDataList();
    },

    methods: {
      showCustomerAddForm: function (data) {
        this.isShowCustomerAddForm = true;
      },
      resetSearch: function () {
        this.search = _.cloneDeep(this.oldSearch);
      },
      searchData: function () {
        this.pagination.currentPage = 1;
        this.setCustomerDataList(true);
      },
      pageChange: function () {
        this.setCustomerDataList();
      },
      setCustomerDataList: function (isSearch) {
        if (!isSearch) {
          this.search = _.cloneDeep(this.oldSearch);
        }

        var pagination = this.pagination;
        var currentPage = pagination.currentPage;
        var pageSize = pagination.pageSize;
        var start = (currentPage - 1) * pageSize;
        var postData = {
          start: start,
          length: pageSize,
          type: customerType
        };

        var search = this.search;
        var customer = search.customer;
        if (customer !== '') {
          postData.customerCode = customer.code;
          postData.customerName = customer.name;
        }

        var fields = ['status', 'paymentWay'];
        SysUtil.setPostData(postData, search, fields, true);
        this.loading = true;
        var self = this;
        return ajaxRequest("basicData/queryCustomersTow", postData, false).then(
          function (res) {
            if (isSearch) {
              self.oldSearch = _.cloneDeep(self.search);
            }

            var customerList = res.returnData;
            for (var i = 0; i < customerList.length; i++) {
              var customer = customerList[i];
              var email = customer.email;
              if (email) {
                email = email.split(';');
              } else {
                email = [];
              }
              customer.email = email;
            }

            self.customerList = customerList;
            self.pagination.totalCount = res.iTotalRecords;
            self.loading = false;
            return self.companyMaterialTypeDataList;
          }).catch(function (error) {
          self.loading = false;
          return [];
        });
      },
      getCustomerDetail: function (row) {
        this.updateCustomer = new Customer(row);
      },
      addCustomerAfter: function () {
        this.isShowCustomerAddForm = false;
        this.setCustomerDataList();
        this.$refs.searchForm.initDropdown();
      },
      updateCustomerAfter: function () {
        this.setCustomerDataList();
      }
    },

    components: {
      CustomerForm: CustomerForm,
      SearchForm: SearchForm,
      SearchBtnView: SearchBtnView
    }
  };
}
